package belf.migrate.api.util;

import belf.migrate.api.common.DatabaseIdentifier;
import belf.migrate.api.taskconf.Task;
import belf.migrate.api.type.DbObjectType;

import java.util.List;

public enum DatabaseType {
    ORACLE(
            DatabaseIdentifier.ORACLE,
            QuoteType.DOUBLE_QUOTE,
            QualifiedType.DOT,
            List.of(
                    DbObjectType.TABLE,
                    DbObjectType.VIEW,
                    DbObjectType.SEQUENCE,
                    DbObjectType.SYNONYM,
                    DbObjectType.PROCEDURE,
                    DbObjectType.FUNCTION,
                    DbObjectType.TYPE,
                    DbObjectType.TRIGGER,
                    DbObjectType.INDEX)),
    MYSQL(
            DatabaseIdentifier.MYSQL,
            QuoteType.BACK_QUOTE,
            QualifiedType.DOT,
            List.of(
                    DbObjectType.TABLE,
                    DbObjectType.VIEW,
                    DbObjectType.PROCEDURE,
                    DbObjectType.FUNCTION,
                    DbObjectType.TRIGGER,
                    DbObjectType.INDEX)),
    DAMENG(
            DatabaseIdentifier.DAMENG,
            QuoteType.DOUBLE_QUOTE,
            QualifiedType.DOT,
            List.of(
                    DbObjectType.TABLE,
                    DbObjectType.VIEW,
                    DbObjectType.SEQUENCE,
                    DbObjectType.SYNONYM,
                    DbObjectType.PROCEDURE,
                    DbObjectType.FUNCTION,
                    DbObjectType.TYPE,
                    DbObjectType.TRIGGER,
                    DbObjectType.INDEX)),
    KINGBASE(
            DatabaseIdentifier.KINGBASE,
            QuoteType.DOUBLE_QUOTE,
            QualifiedType.DOT,
            List.of(
                    DbObjectType.TABLE,
                    DbObjectType.VIEW,
                    DbObjectType.SEQUENCE,
                    DbObjectType.PROCEDURE,
                    DbObjectType.FUNCTION,
                    DbObjectType.TYPE,
                    DbObjectType.TRIGGER,
                    DbObjectType.INDEX)),

    GBASE_8S(
            DatabaseIdentifier.GBASE_8S,
            QuoteType.NONE,
            QualifiedType.COLON,
            List.of(
                    DbObjectType.TABLE,
            DbObjectType.VIEW,
            DbObjectType.SEQUENCE,
            DbObjectType.PROCEDURE,
            DbObjectType.FUNCTION,
            DbObjectType.TYPE,
            DbObjectType.TRIGGER,
            DbObjectType.INDEX)),
    SHENTONG(
            DatabaseIdentifier.SHENTONG,
            QuoteType.NONE,
            QualifiedType.DOT,
            List.of(
                    DbObjectType.TABLE,
                    DbObjectType.VIEW,
                    DbObjectType.SEQUENCE,
                    DbObjectType.SYNONYM,
                    DbObjectType.PROCEDURE,
                    DbObjectType.FUNCTION,
                    DbObjectType.TYPE,
                    DbObjectType.TRIGGER,
                    DbObjectType.INDEX));


    private final String name;

    private final QuoteType quoteType;

    private final QualifiedType qualifiedType;

    private List<DbObjectType> objectTypes;


    DatabaseType(String name, QuoteType quoteType, QualifiedType qualifiedType, List<DbObjectType> objectTypes) {
        this.name = name;
        this.quoteType = quoteType;
        this.qualifiedType = qualifiedType;
        this.objectTypes = objectTypes;
    }

    public String getName() {
        return name;
    }

    public QuoteType getQuoteType() {
        return quoteType;
    }

    public QualifiedType getQualifiedType() {
        return qualifiedType;
    }

    public static QualifiedType getDefaultQualified() {
        return DatabaseType.MYSQL.getQualifiedType();
    }

    public List<DbObjectType> getObjectTypes() {
        return objectTypes;
    }

    public void setObjectTypes(List<DbObjectType> objectTypes) {
        this.objectTypes = objectTypes;
    }

    public static DatabaseType getDatabaseType(String name) {
        for (DatabaseType databaseType : DatabaseType.values()) {
            if (databaseType.getName().equals(name)) {
                return databaseType;
            }
        }
        return ORACLE;
    }

    public static int getVerifyObjectTypeSize(String sourceDb, String sinkDb) {

        int total = 0;
        for (DbObjectType dbObjectType : DbObjectType.values()) {
            DatabaseType sourceDatabaseType = DatabaseType.getDatabaseType(sourceDb);
            DatabaseType sinkDatabaseType = DatabaseType.getDatabaseType(sinkDb);
            if (sourceDatabaseType != null &&
                    sinkDatabaseType != null &&
                    sourceDatabaseType.getObjectTypes().contains(dbObjectType) &&
                    sinkDatabaseType.getObjectTypes().contains(dbObjectType) && !DbObjectType.INDEX.equals(dbObjectType)) {
                total++;
            }
        }
        return total;

    }
}
